package studentsys;

import javax.rmi.CORBA.Util;
import java.util.Scanner;

/**
 * 学生管理系统
 *
 * @author think
 */
public class StudentManager {


    //控制显示菜单
    private boolean k = true;

    //接受用户的选择
    private char key = ' ';

    private StudentService studentService = new StudentService(10);

    int l = 0;


    //主菜单
    public void run() {
        Scanner sc = new Scanner(System.in);
        do {
            System.out.println("--------学生管理系统--------");
            System.out.println("1,查看学生信息");
            System.out.println("2,增加学生信息");
            System.out.println("3,删除学生信息");
            System.out.println("4,修改学生信息");
            System.out.println("5,通过IP查找指定学生");
            System.out.println("6,退出");

            System.out.println("请选择1-5:");
            int key = sc.nextInt();
            switch (key) {
                case 1:
                    listStudent();
                    break;
                case 2:
                    addStudent();
                    break;
                case 3:
                    delHouse();
                    break;
                case 4:
                    update();
                    break;
                case 5:
                    findStudent();
                    break;
                case 6:
                    exit();
                    k = false;
                    break;
                default:
                    System.out.println("选择有误，重新输入");

            }
        }
        while (k);
        System.out.println("------退出学生管理系统-----");

    }

    //显示学生信息列表
    public void listStudent() {
        System.out.println("--------学生信息列表--------");
        System.out.println("学生id\t\t学生姓名\t\t学生年纪");
        Student[] student = studentService.list();
        for (int i = 0; i < student.length; i++) {
            if (student[i] == null) {
                break;
            }
            System.out.println(student[i]);
        }
        System.out.println("-----学生信息列表生成完毕---");
    }

    //增加学生信息

    public void addStudent() {
        System.out.println("-------增加学生信息------");
        System.out.println("姓名: ");
        String name = Utility.readString(8);
        System.out.println("年纪: ");
        int rent = Utility.readInt(100);
        Student newStudent = new Student(0, name, rent);
        if (studentService.add(newStudent)) {
            System.out.println("添加成功");
        } else
            System.out.println("添加失败");

    }

    //删除学生信息
    public void delHouse() {
        System.out.println("=========删除学生信息=========");
        System.out.println("请输入待删除学生的编号（-1退出）");
        int delId = Utility.readInt();
        if (delId == -1) {
            System.out.println("==========放弃删除学生信息=========");
            return;
        }

        char choice = Utility.readConfirmSelection();

        if (choice == 'Y') {//确定删除
            if (studentService.del(delId)) {
                System.out.println("=========删除学生信息成功=========");

            } else {
                System.out.println("=========学生编号不存在，删除失败");
            }
        } else {
            System.out.println("=========放弃删除学生信息=========");
        }
    }

    //修改
    public void update(){
        System.out.println("=========修改学生信息=========");
        System.out.println("请选择待修学生编号（-1表示退出）");
        int updateId = Utility.readInt();
        if (updateId == -1){
            System.out.println("=========放弃修改学生信息=========");
            return;
        }

        Student student = studentService.findById(updateId);

        if (student == null) {
            System.out.println("=========修改学生信息编号不存在=========");
            return;
        }

        System.out.print("姓名(" + student.getName() + "): ");
        //这里如果用户直接回车表示不修改信息 默认""
        String name = Utility.readString(8, "");
        if (!"".equals(name)) {//修改
            student.setName(name);
        }
        System.out.print("年龄(" + student.getAge() + "):");
        int year = Utility.readInt(-1);
        if (year != -1) {
            student.setAge(year);
        }

        System.out.println("===========修改学生信息成功=========");
    }


    //根据id查找学生信息
    public void findStudent(){
        System.out.println("=========查找房屋信息=========");
        System.out.println("请输入要查找的id");
        int findId = Utility.readInt();
        //调用方法
        Student house = studentService.findById(findId);
        if (house != null){
            System.out.println(house);
        }else {
            System.out.println("=========查找学生信息id不存在=========");
        }
    }


    public void exit(){
        char c = Utility.readConfirmSelection();
        if (c == 'Y'){
            k = false;
        }
    }


}
